<?php

/**
 * This is the model class for table "{{merchant}}".
 *
 * The followings are the available columns in table '{{merchant}}':
 * @property integer $id
 * @property string $name
 * @property string $address
 * @property string $phone
 * @property string $fax
 * @property string $companyQQ
 * @property string $companyEmail
 * @property string $otherName
 * @property string $bossName
 * @property string $bossPhone
 * @property string $contactName
 * @property string $contactPhone
 * @property string $serviceScope
 * @property string $note
 * @property string $createTime
 * @property integer $createUserId
 * @property string $updateTime
 * @property integer $updateUserId
 *
 * The followings are the available model relations:
 * @property User $updateUser
 * @property User $createUser
 */
class Merchant extends CActiveRecord {
	/**
	 * Returns the static model of the specified AR class.
	 * @return Merchant the static model class
	 */
	public static function model($className = __CLASS__) {
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName() {
		return '{{merchant}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules() {
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array(
				'name',
				'required'
			),
			array(
				'name, address, phone, fax, companyQQ, companyEmail, otherName, bossName, bossPhone, contactName, contactPhone, serviceScope, note',
				'length',
				'max' => 255
			),
			array(
				'companyEmail',
				'email',
			),
			array(
				'name',
				'unique',
			),
			//array('createTime, updateTime', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array(
				'id, name, address, phone, fax, otherName, bossName, bossPhone, contactName, contactPhone, serviceScope, note, createTime, createUserId, updateTime, updateUserId',
				'safe',
				'on' => 'search'
			),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations() {
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'updateUser' => array(
				self::BELONGS_TO,
				'User',
				'updateUserId'
			),
			'createUser' => array(
				self::BELONGS_TO,
				'User',
				'createUserId'
			),
		);
	}

	public function behaviors() {
		return array(
			'AuditableBehavior' => array(
				'class' => 'components.db.AuditableBehavior',
				'attributes' => 'name, address, phone, fax, companyQQ, companyEmail, otherName, bossName, bossPhone, contactName, contactPhone, serviceScope, note'
			),
			'AuditStampBehavior' => array(
				'class' => 'components.db.AuditStampBehavior',
			),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels() {
		return array(
			'id' => 'ID',
			'name' => '名称',
			'address' => '地址',
			'phone' => '电话',
			'fax' => '传真',
			'companyQQ' => '公司QQ',
			'companyEmail' => '公司电邮',
			'otherName' => '其它名称',
			'bossName' => '老板名字',
			'bossPhone' => '老板电话',
			'contactName' => '经办人',
			'contactPhone' => '经办人电话',
			'serviceScope' => '经营范围',
			'note' => '备注',
			'createTime' => '创建时间',
			'createUserId' => '创建用户',
			'updateTime' => '更新时间',
			'updateUserId' => '更新用户',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search() {
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria = new CDbCriteria;

		$criteria->compare('id', $this->id);
		$criteria->compare('name', $this->name, true);
		$criteria->compare('address', $this->address, true);
		$criteria->compare('phone', $this->phone, true);
		$criteria->compare('fax', $this->fax, true);
		$criteria->compare('companyQQ', $this->companyQQ, true);
		$criteria->compare('companyEmail', $this->companyEmail, true);
		$criteria->compare('otherName', $this->otherName, true);
		$criteria->compare('bossName', $this->bossName, true);
		$criteria->compare('bossPhone', $this->bossPhone, true);
		$criteria->compare('contactName', $this->contactName, true);
		$criteria->compare('contactPhone', $this->contactPhone, true);
		$criteria->compare('serviceScope', $this->serviceScope, true);
		$criteria->compare('note', $this->note, true);
		$criteria->compare('createTime', $this->createTime, true);
		$criteria->compare('createUserId', $this->createUserId);
		$criteria->compare('updateTime', $this->updateTime, true);
		$criteria->compare('updateUserId', $this->updateUserId);

		return new CActiveDataProvider($this, array(
			'criteria' => $criteria,
		));
	}
	
	public function getDisplayName(){
		if($this->otherName){
			return $this->name . ' ('.$this->otherName.')';
		}
		return $this->name;
	}
}
